package mysql

import (
	"box-mart-api/boxrpc/boxrpc"
	"fmt"

	"gorm.io/gorm"
)

type Box struct {
	gorm.Model
	BoxTitle string `gorm:"type:varchar(50)"`
	BoxName  string `gorm:"type:varchar(50)"`
	BoxPrice int64  `gorm:"type:int(11)"`
	BoxType  string `gorm:"type:varchar(50)"`
}

func (b *Box) BoxAdd(db *gorm.DB) error {
	return DB.Debug().Create(b).Error
}

func (b *Box) BoxDetail(db *gorm.DB, id int64) error {
	return DB.Debug().Where("id=?", id).First(b).Error
}
func (b *Box) BoxList(db *gorm.DB) error {
	return DB.Debug().Find(&b).Error
}

func (b *Box) BoxDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(b).Error
}

func (b *Box) BoxUpdate(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Updates(b).Error
}
func BoxList(db *gorm.DB, in *boxrpc.BoxListRequest) ([]*boxrpc.BoxList, error) {
	var list []*boxrpc.BoxList
	err2 := db.Model(&Box{}).Select("boxes.id as box_id", "title", "name", "price", "type").Find(&list).Error
	fmt.Println("000", list)
	return list, err2
}
